package cn.da.shuai.cool.system.service.service.impl;

import cn.da.shuai.cool.mybatis.service.impl.CoolServiceImpl;
import cn.da.shuai.cool.system.service.mapper.CoolDictDataMapper;
import cn.da.shuai.cool.system.model.entity.dict.CoolDictData;
import cn.da.shuai.cool.system.service.service.ICoolDictDataService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典数据服务实现类
 */
@Service
public class CoolDictDataServiceImpl extends CoolServiceImpl<CoolDictDataMapper, CoolDictData> implements ICoolDictDataService {

    @Override
    public Page<CoolDictData> selectDictDataPage(CoolDictData dictData, Page<CoolDictData> page) {
        LambdaQueryWrapper<CoolDictData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(dictData.getDictType()), CoolDictData::getDictType, dictData.getDictType())
           .like(StrUtil.isNotBlank(dictData.getDictLabel()), CoolDictData::getDictLabel, dictData.getDictLabel())
           .eq(StrUtil.isNotBlank(dictData.getStatus()), CoolDictData::getStatus, dictData.getStatus())
           .orderByAsc(CoolDictData::getDictSort);
        return this.page(page, lqw);
    }

    @Override
    public List<CoolDictData> selectDictDataList(CoolDictData dictData) {
        LambdaQueryWrapper<CoolDictData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(dictData.getDictType()), CoolDictData::getDictType, dictData.getDictType())
           .like(StrUtil.isNotBlank(dictData.getDictLabel()), CoolDictData::getDictLabel, dictData.getDictLabel())
           .eq(StrUtil.isNotBlank(dictData.getStatus()), CoolDictData::getStatus, dictData.getStatus())
           .orderByAsc(CoolDictData::getDictSort);
        return this.list(lqw);
    }

    @Override
    public List<CoolDictData> selectDictDataByType(String dictType) {
        LambdaQueryWrapper<CoolDictData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CoolDictData::getDictType, dictType)
           .eq(CoolDictData::getStatus, "0")
           .orderByAsc(CoolDictData::getDictSort);
        return this.list(lqw);
    }

    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        LambdaQueryWrapper<CoolDictData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CoolDictData::getDictType, dictType)
           .eq(CoolDictData::getDictValue, dictValue);
        CoolDictData data = this.getOne(lqw);
        return data != null ? data.getDictLabel() : "";
    }

    @Override
    public CoolDictData selectDictDataById(Long dictCode) {
        return this.getById(dictCode);
    }

    @Override
    public void deleteDictDataByIds(Long[] ids) {
        this.removeByIds(List.of(ids));
    }

    @Override
    public int insertDictData(CoolDictData data) {
        return this.save(data) ? 1 : 0;
    }

    @Override
    public int updateDictData(CoolDictData data) {
        return this.updateById(data) ? 1 : 0;
    }

    @Override
    public long countDictDataByType(String dictType) {
        return this.count(new LambdaQueryWrapper<CoolDictData>().eq(CoolDictData::getDictType, dictType));
    }

    @Override
    public int updateDictDataType(String oldDictType, String newDictType) {
        LambdaUpdateWrapper<CoolDictData> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CoolDictData::getDictType, oldDictType)
               .set(CoolDictData::getDictType, newDictType);
        return this.update(wrapper) ? 1 : 0;
    }
}